u $datadir/subsidy_data, clear 
keep id naics2 n_bid 
keep if naics2 <40 //manufacturing 
duplicates drop 
 local N = _N 

*mixture over bidders: 
count if n_bid==2
global n_2 = `r(N)'/`N'
count if n_bid==3
global n_3 =`r(N)'/`N' 
count if inrange(n_bid, 4,5)
global n_5 = `r(N)'/`N'
count if n_bid>5
global n_10 = `r(N)'/`N'

foreach v in varv corr  {
foreach i in 2 3 5 10 {
	insheet using $datadir/sim/results_`v'`i'.csv, comma clear

	g group=floor((v1-.5)/1000)

	g pos_w=(delta_w>0)
	drop if subsidy<-100 
	bys group: egen perc_welfare_gain = mean(pos_w)
	
	g welfare_gain=delta_w/(v0+max_pi)
	replace welfare_gain=0 if welfare_gain<0
	g firm_payoff =subsidy + win_pi
	g state_payoff = win_v - subsidy
		
	winsor2 welfare_gain, cut(0 99) replace
	replace firm_payoff=. if delta_w==.
	replace state_payoff=. if delta_w==.
	
	rename * *`i'
	rename group group
	rename v1 v1
	
	tempfile `v'all`i'
	sa ``v'all`i''

	collapse (mean) delta_w firm_payoff state_payoff win_v win_pi v0 max_pi`i' welfare_gain, by(group perc_welfare_gain)
	tempfile `v'`i'
	sa ``v'`i''
}


u ``v'2', clear
	foreach i in 3 5 10 {
		merge 1:1 group using ``v'`i'', keep(3) nogen
	}

sa $datadir/sim/`v', replace

	u ``v'all2', clear
	foreach i in 3 5 10 {
		merge 1:1 v1 group using ``v'all`i'', keep(3) nogen
	}

sa $datadir/sim/`v'_all, replace
}

u $datadir/sim/corr_all, clear 
g rho=0
forvalues i = 0/50 {
		qui corr win_pi2 win_v2  if group==`i'
		replace rho=r(rho) if group==`i'
}
collapse (mean) rho, by(group)
sa $temp/rho, replace 
**********************************************************************************/
set scheme s1mono

u $datadir/sim/corr, clear
merge 1:1 group using $temp/rho 

g corr=.
replace corr = rho if group==0
replace corr= .3 if group==50 
ipolate corr group, gen(correlation)

g welfare_gain_mixture = welfare_gain2*$n_2 + ///
	welfare_gain3*$n_3 + ///
	welfare_gain5*$n_5 + ///
	welfare_gain10*$n_10
	
g norm_welfare = welfare_gain_mixture in 28 
summ norm_welfare 
replace norm_welfare = welfare_gain_mixture/`r(mean)'

	g firm_gain_mixture = firm_payoff2*$n_2 + ///
	firm_payoff3*$n_3 + ///
	firm_payoff5*$n_5 + ///
	firm_payoff10*$n_10

		g state_gain_mixture = state_payoff2*$n_2 + ///
	state_payoff3*$n_3 + ///
	state_payoff5*$n_5 + ///
	state_payoff10*$n_10

	g firm_share = firm_gain_mix/(firm_gain_mix + state_gain_mix)
	
g norm_firm = firm_share in 28 
summ norm_firm 
replace norm_firm = firm_share/`r(mean)'	

twoway (scatter perc_welfare_gain2 perc_welfare_gain3 perc_welfare_gain5 ///
	correlation, c(l l l) msize(small small small) m(Oh Sh Dh) mcol(gray gs5 black) lcol(gray gs4  black) ///
	lpattern(solid solid solid)) if inrange(correlation, -.5,.31), xtitle("correlation between v and {&pi}") ///
	xlabel(-.5(.1).3) legend(order(- "bidders:" 1 "2" 2 "3"  3 "5") ///
	col(4) region(lstyle(none))) ylabel(0(.1)1) xline(-.07, lpattern(dash) lcolor(red)) 
graph export $appxdir/corr_percgain.eps, replace

twoway (scatter norm_welfare correlation,c(l ) msize( small) m(Oh)  ///
	lpattern(solid)) if inrange(correlation, -.5,.31), xtitle("correlation between v and {&pi}") ytitle("relative welfare gain") ///
	xlabel(-.5(.1).3) legend(off) ylabel(0(.5)2.75) xline(-0.07, lpattern(dash) lcolor(red)) 
graph export $appxdir/corr_gain.eps, replace

twoway (scatter norm_firm correlation,c(l ) msize( small) m(Oh)  ///
	lpattern(solid)) if inrange(correlation, -.5,.31), xtitle("correlation between v and {&pi}") ytitle("relative firm share of welfare") ///
	xlabel(-.5(.1).3) legend(off) ylabel(.5(.25)2) xline(-0.07, lpattern(dash) lcol(red))
graph export $appxdir/corr_firm.eps, replace

**********************************************************************************
u $datadir/sim/varv, clear
g variance = group/10

g perc_mixture = perc_welfare_gain2*$n_2 + ///
	perc_welfare_gain3*$n_3 + ///
	perc_welfare_gain5*$n_5 + ///
	perc_welfare_gain10*$n_10
	
	g welfare_gain_mixture = welfare_gain2*$n_2 + ///
	welfare_gain3*$n_3 + ///
	welfare_gain5*$n_5 + ///
	welfare_gain10*$n_10
	
	g firm_gain_mixture = firm_payoff2*$n_2 + ///
	firm_payoff3*$n_3 + ///
	firm_payoff5*$n_5 + ///
	firm_payoff10*$n_10

		g state_gain_mixture = state_payoff2*$n_2 + ///
	state_payoff3*$n_3 + ///
	state_payoff5*$n_5 + ///
	state_payoff10*$n_10

	g firm_share = firm_gain_mix/(firm_gain_mix + state_gain_mix)
	
g norm_welfare = welfare_gain_mixture in 20
summ norm_welfare 
replace norm_welfare = welfare_gain_mixture/`r(mean)'

g norm_firm = firm_share in 20 
summ norm_firm 
replace norm_firm = firm_share/`r(mean)'

twoway (scatter perc_welfare_gain2 perc_welfare_gain3 perc_welfare_gain5 ///
	variance, c(l l l) msize(small small small) m(Oh Sh Dh) mcol(gray gs5 black) lcol(gray gs4  black) ///
	lpattern(solid solid solid)) if inrange(variance,.95,3.05), xtitle("log(sd) of v") ///
	xlabel(1(.25)3) legend(order(- "bidders:" 1 "2" 2 "3" 3 "5") ///
	col(4) region(lstyle(none))) ylabel(0(.1)1) xline(2.08, lpattern(dash) lcol(red))
graph export $appxdir/varv_percgain.eps, replace

twoway (scatter norm_welfare variance,c(l ) msize( small) m(Oh)  ///
	lpattern(solid)) if inrange(variance,.95,3.05), xtitle("log(sd) of v") ytitle("relative welfare gain") ///
	xlabel(1(.25)3) legend(off) ylabel(0(.5)2.75) xline(2.08, lpattern(dash) lcol(red))
graph export $appxdir/varv_gain.eps, replace

twoway (scatter norm_firm variance,c(l ) msize( small) m(Oh)  ///
	lpattern(solid)) if inrange(variance,.95,3.05), xtitle("log(sd) of v") ytitle("relative firm share of welfare") ///
	xlabel(1(.25)3) legend(off) ylabel(.5(.5)4.5) xline(2.08, lpattern(dash) lcol(red))
graph export $appxdir/varv_firm.eps, replace

